package 剑指offer;

public class Offer43_JZ30 {
    public int maxSubArray(int[] nums) {
        int front = nums[0];
        int max = front;
        for (int i=1; i<nums.length; i++) {
            if (nums[i] + front < nums[i]) {
                front = nums[i];
            } else {
                front = nums[i] + front;
            }
            if (front > max) {
                max = front;
            }
        }
        return max;
    }

    public int maxSubArray2(int[] nums) {
        int max = nums[0];
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        for(int i=1; i<nums.length; i++) {
            dp[i] = Math.max(dp[i-1] + nums[i], nums[i]);
            if(dp[i] > max) {
                max = dp[i];
            }
        }
        return max;
    }
}
